import { useReducer } from 'react';

function App() {
  const [count, dispatch] = useReducer((state, action) => {
    //dispatch传参action state就是count
    if (action === 'add') {
      return state + 1;
    } else if (action === 'sub') {
      return state - 1;
    } else {
      return state;
    }
  }, 1);
  return (
    <div>
      数量：{count}
      <br />
      <button onClick={() => dispatch('add')}>+1</button>
      <br />
      <button onClick={() => dispatch('sub')}>-1</button>
      <br />
    </div>
  );
}

export default App;
